//
//  InfoListCell.swift
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/13.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

import UIKit

//行形式
@objc protocol InfoListRowsable {
    var items: [InfoListRowItemable] { get }
}

@objc protocol InfoListRowItemable {
    var titles: [String] { get }
}

@objc protocol InfoListViewDatasource: class {
    func numberOfSections(in listView: InfoListView) -> Int

    func listView(_ listView: InfoListView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

    @objc optional func listView(_ listView: InfoListView, heightForRowAt indexPath: IndexPath) -> CGFloat

    /// 获取每个section的标题和副标题
    /// - Returns: index[0] 标题  index[1] 副标题
    func listView(_ listView: InfoListView, headerTextsInSection section: Int) -> [String]
}

@objcMembers
class InfoListView: UIView {

    var datasource: InfoListViewDatasource!

    lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.registerCellClass(with: InfoListFormCell.self)
        tableView.registerCellClass(with: InfoListRowsCell.self)
        tableView.registerHeaderFooterClass(with: InfoListViewHeader.self)
        tableView.tableFooterView = UIView()
        tableView.showsVerticalScrollIndicator = false
        tableView.estimatedRowHeight = 200
        tableView.allowsSelection = false
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    private func setup() {
        addSubview(tableView)
        tableView.mas_makeConstraints { (make) in
            make?.edges.equalTo()(self)
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        tableView.reloadData()
    }
}

extension InfoListView: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return datasource.listView?(self, heightForRowAt: indexPath) ??  UITableViewAutomaticDimension
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        let titles = datasource.listView(self, headerTextsInSection: section)
        return (titles.count > 1 && !titles[1].isEmpty) ? 66 : 42
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return datasource.numberOfSections(in: self)
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header: InfoListViewHeader = tableView.dequeueReusableHeaderFooter()
        let titles = datasource.listView(self, headerTextsInSection: section)
        if !titles.isEmpty {
            header.title = titles[0]
        }
        if titles.count > 1 {
            header.subtitle = titles[1]
        }
        return header
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = datasource.listView(self, cellForRowAt:indexPath)
        return cell
    }
}

class InfoListViewHeader: UITableViewHeaderFooterView {

    var title: String? {
        didSet {
            headLineView.label.text = title
        }
    }

    var subtitle: String? {
        didSet {
            detailLabel.text = subtitle
        }
    }

    let headLineView = HeadlineView()

    let detailLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .left
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: 10)
        label.textColor = UIColor(hexString: "#363636");
        return label
    }()

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)

        contentView.backgroundColor = .white
        contentView.addSubview(headLineView)
        contentView.addSubview(detailLabel)

        headLineView.mas_makeConstraints { (make) in
            make?.centerX.equalTo()(self)
            make?.top.equalTo()(self.contentView)?.offset()(10)
        }

        detailLabel.mas_makeConstraints { (make) in
            make?.left.equalTo()(self.contentView)
            make?.height.equalTo()(14)
            make?.top.equalTo()(headLineView.mas_bottom)?.offset()(8)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

